import sys
from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout
from PyQt5.QtCore import Qt, QMimeData, pyqtSignal
from PyQt5.QtGui import QPixmap, QPainter, QDrag, QCursor


# 自定义可拖拽的QLabel
class DraggableLabel(QLabel):
    move_ = pyqtSignal(int, int, int)
    def __init__(self, i, width, height=40, down_c='#6690FF', up_c='#254EDB', dable=False, parent=None):
        super(DraggableLabel, self).__init__('', parent)
        # 设置宽度和高度
        self.id = i
        self.down_color = down_c
        self.up_color = up_c
        self.dable = dable
        self.setFixedSize(width, height)
        # self.setAlignment(Qt.AlignCenter)
        self.setUi()

    def setUi(self):
        # 设置背景颜色和圆角
        self.setStyleSheet(f"background-color: {self.up_color}; border-radius: 10px;")

    # 重写鼠标按下事件
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.drag_start_position = event.pos()

    # 重写鼠标移动事件
    def mouseMoveEvent(self, event):
        if not (event.buttons() & Qt.LeftButton):
            return
        if (event.pos() - self.drag_start_position).manhattanLength() < QApplication.startDragDistance():
            return

        if self.dable:
            # 隐藏
            self.hide()
            drag = QDrag(self)
            mime_data = QMimeData()

            # 设置拖拽的数据类型
            mime_data.setText("这是一个文本例子")  # 可以根据需要更改数据类型
            drag.setMimeData(mime_data)

            # 设置图标
            pixmap = QPixmap(self.size())
            painter = QPainter(pixmap)
            # 绘制当前控件的图像并且加上圆角
            self.setStyleSheet(f"background-color: {self.down_color}; border-radius: 10px;")
            painter.drawPixmap(self.rect(), self.grab())
            painter.end()
            drag.setHotSpot(event.pos() - self.rect().topLeft())
            drag.setPixmap(pixmap)
            # drag.actionChanged.connect(self.on_drag_action_changed)
            # 设置拖拽时的自定义光标
            custom_cursor_pixmap = QPixmap(8, 8)  # 创建一个 32x32 的光标图标
            custom_cursor_pixmap.fill(Qt.red)  # 填充红色
            drag.setDragCursor(custom_cursor_pixmap, Qt.MoveAction)
            # 启动拖拽操作
            drag.exec_()
            self.setStyleSheet(f"background-color: {self.up_color}; border-radius: 10px;")
            self.show()
            pos = QCursor.pos()
            self.move_.emit(self.id, pos.x(), pos.y())

        
    


# 主窗口
class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        layout = QVBoxLayout()
        self.label = DraggableLabel(1, 200, 50, '#6690FF', '#254EDB', True)
        self.label.move_.connect(self.showpos)
        layout.addWidget(self.label)
        self.setAcceptDrops(True)
        self.setLayout(layout)

    def showpos(self, x, y):
        print(x, y)

    # def dragMoveEvent(self, event):
    #     # 在主窗口内拖拽时，总是接受事件
    #     event.accept()

    # def dropEvent(self, event):
    #     # 处理放下事件，例如将图片放置在标签上
    #     event.accept()

    def dragEnterEvent(self, event):
        # 当拖拽进入主窗口时，如果携带了图片数据，则接受拖拽
        event.accept()
    #
    # # 重写主窗口的鼠标事件
    # def mousePressEvent(self, event):
    #     # 将事件传递给子控件
    #     child = self.childAt(event.pos())
    #     if child:
    #         child.mousePressEvent(event)
    #
    # def mouseMoveEvent(self, event):
    #     # 将事件传递给子控件
    #     child = self.childAt(event.pos())
    #     if child:
    #         child.mouseMoveEvent(event)
    #
    # def changeCursor(self, cursor):
    #     self.setCursor(cursor)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())